//  引入axios
import axios from 'axios'
import qs from 'qs'
import local from '@/utils/local.js'
import { Message } from 'element-ui'

// 设置公共ip
axios.defaults.baseURL = 'http://172.16.20.167:5000'
axios.defaults.timeout = 5000

// 请求拦截器
axios.interceptors.request.use(config => {
  const token = local.get('token')
  if (token) {
    config.headers.Authorization = token
  }
  return config
})
// 响应拦截器
axios.interceptors.response.use(res => {
  if (res.status === 200 && res.data.code === 0) {
    // 成功提示信息
    Message.success(res.data.msg)
  } else if (res.data.msg) {
    // 失败提示信息
    Message.error(res.data.msg)
  }
  return res.data
})

export default {
  get(url, params) {
    return new Promise((resolve, reject) => {
      axios
        .get(url, { params })
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          console.log(err)
        })
    })
  },
  post(url, query) {
    return new Promise((resolve, reject) => {
      axios
        .post(url, qs.stringify(query))
        .then(res => {
          resolve(res)
        })
        .catch(err => {
          console.log(err)
        })
    })
  }
}
